//
//  ManageAddressViewController.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/15.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

class ManageAddressViewController: BaseViewController {

    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initBaseLayout()
        layoutPageSubviews()
        configUI()
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // MARK: - event response
    func addAddressAction(sender: UIButton) {
        let addAddressVC = AddAddressViewController()
        addAddressVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(addAddressVC, animated: true)
    }
    
    // MARK: - private mehtod
    func initBaseLayout() {
        self.view.addSubview(tableView)
        self.view.addSubview(addAddressBtn)
        self.view.addSubview(backView)
        self.view.addSubview(addressImageView)
        self.view.addSubview(noAddressLabel)
        self.view.addSubview(addBtn)
    }
    
    func layoutPageSubviews() {
        tableView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view).inset(UIEdgeInsets(top: 0, left: 0, bottom: 44, right: 0))
        }
        addAddressBtn.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(0)
            make.bottom.equalTo(self.view).offset(0)
            make.right.equalTo(self.view).offset(0)
            make.height.equalTo(44)
        }
        
        backView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.centerY.equalTo(self.view)
            make.width.equalTo(100)
            make.height.equalTo(158)
        }
        
        addressImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(backView.snp.top).offset(0)
            make.width.height.equalTo(88)
        }
        
        noAddressLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(addressImageView.snp.bottom).offset(1)
            make.width.equalTo(96)
            make.height.equalTo(20)
        }
        
        addBtn.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(noAddressLabel.snp.bottom).offset(18)
            make.width.equalTo(102)
            make.height.equalTo(32)
        }
        
    }
    
    func configUI() {
        self.view.backgroundColor = UIColor.white
        initNavItem(controller: self, imageName: "nav_back_n", highlitedImageName: "nav_back_h", selectedImageName: "", isLeft: true)
        self.title = "管理收货地址"
        
//        tableView.isHidden = true
//        addAddressBtn.isHidden = true
        addressImageView.isHidden = true
        noAddressLabel.isHidden = true
        addBtn.isHidden = true
    }
    
    // MARK: - setters and getters
    var _tableview: UITableView!
    var tableView: UITableView {
        if _tableview == nil {
            _tableview = UITableView(frame: CGRect.zero, style: .grouped)
            _tableview.backgroundColor = UIColor(hexString: "#E5E5E5")
            _tableview.separatorStyle = .none
            _tableview.delegate = self
            _tableview.dataSource = self
            _tableview.register(ManageAddressCell.self, forCellReuseIdentifier: "ManageAddressCell")
        }
        return _tableview
    }
    
    var _addAddressBtn: UIButton!
    var addAddressBtn: UIButton {
        if _addAddressBtn == nil {
            _addAddressBtn = UIButton()
            _addAddressBtn.backgroundColor = UIColor(hexString: "#B7A18A")
            _addAddressBtn.setTitle("新增收货地址", for: .normal)
            _addAddressBtn.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .normal)
            _addAddressBtn.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 15)
            _addAddressBtn.addTarget(self, action: #selector(ManageAddressViewController.addAddressAction(sender:)), for: .touchUpInside)
        }
        return _addAddressBtn
    }
    
    var _backView: UIView!
    var backView: UIView {
        if _backView == nil {
            _backView = UIView()
            _backView.backgroundColor = UIColor.clear
        }
        return _backView
    }
    
    var _addressImageView: UIImageView!
    var addressImageView: UIImageView {
        if _addressImageView == nil {
            _addressImageView = UIImageView()
            _addressImageView.image = UIImage(named: "icon_addresswu")
        }
        return _addressImageView
    }
    
    var _noAddressLabel: UILabel!
    var noAddressLabel: UILabel {
        if _noAddressLabel == nil {
            _noAddressLabel = UILabel()
            _noAddressLabel.font = UIFont(name: "PingFang-SC-Regular", size: 12)
            _noAddressLabel.textColor = UIColor(hexString: "#999999")
            _noAddressLabel.textAlignment = .center
            _noAddressLabel.text = "您还没有收货地址"
        }
        return _noAddressLabel
    }
    
    var _addBtn: UIButton!
    var addBtn: UIButton {
        if _addBtn == nil {
            _addBtn = UIButton()
            _addBtn.setTitle("新增收货地址", for: .normal)
            _addBtn.backgroundColor = UIColor.white
            _addBtn.setTitleColor(UIColor(hexString: "#252525"), for: .normal)
            _addBtn.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 12)
            _addBtn.layer.borderWidth = 0.5
            _addBtn.layer.borderColor = UIColor(hexString: "#B7B7B7")?.cgColor
        }
        return _addBtn
    }
}

extension ManageAddressViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let string = "ManageAddressCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: string, for: indexPath) as! ManageAddressCell
        
        cell.configUI()
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 138
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if section == 4 {
            return 0.1
        }
        return 16
    }
}
